import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

public class IncomeTableModel extends AbstractTableModel {
	public static final int COL_DESCR = 0;
	public static final int COL_PRICE = 1;
	public static final int COL_DATE = 2;

	protected Wallet w;
	protected Vector <Income> incomeList;

	private String[] columnNames = {"Description", "Amount", "Date"};

	public IncomeTableModel() {
		w = null;
		incomeList = null;
	}    	

	public int getRowCount() {
		return incomeList == null ? 0 : incomeList.size();
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public boolean isCellEditable(int nRow, int nCol) {
		return true;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int nRow, int nCol) {
		if (nRow < 0 || nRow>=getRowCount())
			return "";
		Income row = (Income)incomeList.elementAt(nRow);

		switch (nCol) {
		case COL_DESCR: return row.getDescription();
		case COL_PRICE: return row.getAmt();
		case COL_DATE: return row.getDate().getTime();
		}
		return "";
	}

	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's
	 * data can change.
	 */
	public void setValueAt(Object value, int nRow, int nCol) {
		if (nRow < 0 || nRow>=getRowCount())
			return;
		Income row = (Income) incomeList.elementAt(nRow);
		String svalue = value.toString();

		switch (nCol) {
			case COL_DESCR: 
				w.editIncome(svalue, row.getDate(), row.getAmt(), row);
				break;
			case COL_PRICE:
				w.editIncome(row.getDescription(), row.getDate(), new BigDecimal(svalue), row);
				break;
			case COL_DATE:
				w.editIncome(row.getDescription(), (Calendar)value, row.getAmt(), row);
				break;
		}
	}

	public void setWallet(Wallet w){
		this.w = w;
		incomeList = w.getIncomeList();
	}
	
	public Vector <Income> getIncomeList() {
		return incomeList;
	}
}